package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.model.Emp;
import com.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//响应文本
//@ResponseBody
//@Controller

@RequestMapping("/emp")
@RestController
public class EmpController {

    //  列表数据  支持分页和条件查询
    @Autowired
    private EmpService empService;
    //列表数据
    @RequestMapping("/list")
    public Page<Emp> list(String enameS, Long pageNum, Long size){
        //条件对象
//        QueryWrapper<Emp> sql = new QueryWrapper<Emp>();
//        sql.like("ename",enameS);
        //List<Emp> emps = empDao.selectList(sql);
        //分页对象
        Page<Emp> page = new Page<Emp>(pageNum, size);
        page = empService.listPage(page,new Emp(0,enameS));
        return page;
    }


    //列表数据
    @RequestMapping("/listAll")
    public List<Emp> listAll(){
        List<Emp> list = empService.list();
        return list;
    }

    //支持新增或修改 Post提交  /emp/edit
    @RequestMapping("/edit")
    public boolean edit(@RequestBody Emp emp){
        boolean res = false;
        if("0".equals(emp.getModify())){
            //新增
            res =empService.save(emp);
        }else{
            //修改
            res = empService.updateById(emp);
        }
        return res;
    }

    @RequestMapping("/getByEmpno")
    public Emp getByEmpno(int empno){
        return empService.getById(empno);
    }

    @RequestMapping("/del")
    public boolean del(Emp emp){
        return empService.removeById(emp.getEmpno());
    }
}
